package com.local.order.controller;

import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.order.pojo.Order;
import com.local.order.pojo.OrderItem;
import com.local.order.pojo.OrderListRequestParam;
import com.local.order.pojo.OrdersRequestParam;
import com.local.order.service.OrderItemService;
import com.local.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    /**
     * 查询订单列表，支持翻页（后台）
     * @param requestParam
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil<PageResultUtil<Order>> list(@RequestBody OrderListRequestParam requestParam) {
        Integer page = requestParam.getPage();
        Integer limit = requestParam.getLimit();
        String sort = requestParam.getSort();
        String order = requestParam.getOrder();
        Integer isDeleted = requestParam.getIsDeleted();

        page = Objects.isNull(page) || page < 1 ? 1 : page;
        limit = Objects.isNull(limit) || limit < 1 ? 10 : limit;
        sort = sort.isEmpty() ? "order_id" : sort;
        order = order.isEmpty() ? "desc" : order;

        PageResultUtil<Order> orderList = orderService.findOrderList(page, limit, sort, order, isDeleted);
        if (Objects.nonNull(orderList)) {
            return new ResultUtil(200, "", orderList);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }

    /**
     * 修改（后台）
     * @param order
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody Order order) {
        Integer updateResult = orderService.update(order);
        if (updateResult > 0) {
            return new ResultUtil(200, "修改成功");
        } else {
            return new ResultUtil(201, "修改失败");
        }
    }


    /**
     * 批量修改订单状态为：配货完成（后台）
     * @param ids
     * @return
     */
    @PostMapping("/checkDone")
    @ResponseBody
    public ResultUtil checkDone(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return new ResultUtil(201, "参数错误");
        }

        String resultMessage = orderService.checkDone(ids);
        if (resultMessage.isEmpty()) {
            return new ResultUtil(200, "配货操作成功");
        } else {
            return new ResultUtil(201, resultMessage);
        }
    }


    /**
     * 批量修改订单状态为：出库完成（后台）
     * @param ids
     * @return
     */
    @PostMapping("/checkOut")
    @ResponseBody
    public ResultUtil checkOut(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return new ResultUtil(201, "参数错误");
        }

        String resultMessage = orderService.checkOut(ids);
        if (resultMessage.isEmpty()) {
            return new ResultUtil(200, "出库操作成功");
        } else {
            return new ResultUtil(201, resultMessage);
        }
    }


    /**
     * 批量修改订单状态为：商家关闭（后台）
     * @param ids
     * @return
     */
    @PostMapping("/close")
    @ResponseBody
    public ResultUtil close(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return new ResultUtil(201, "参数错误");
        }

        String resultMessage = orderService.close(ids);
        if (resultMessage.isEmpty()) {
            return new ResultUtil(200, "关闭订单成功");
        } else {
            return new ResultUtil(201, resultMessage);
        }
    }


    /**
     * 查询订单的所有商品明细（后台）
     * @param orderId
     * @return
     */
    @GetMapping("/items/{orderId}")
    @ResponseBody
    public ResultUtil items(@PathVariable("orderId") Integer orderId) {
        if (orderId <= 0) {
            return new ResultUtil(201, "参数错误");
        }

        List<OrderItem> orderItems = orderItemService.selectByOrderId(orderId);
        if (Objects.nonNull(orderItems)) {
            return new ResultUtil(200, "", orderItems);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }


    /**
     * 生成新订单
     * @param order
     * @return
     */
    @PostMapping("/saveOrder")
    @ResponseBody
    public ResultUtil<Order> saveOrder(@RequestBody Order order) {
        ResultUtil<Order> saveResult = orderService.saveOrder(order);
        return saveResult;
    }


    /**
     * 批量添加OrderItems
     * @param orderItemList
     * @return
     */
    @PostMapping("/saveOrderItems")
    @ResponseBody
    public ResultUtil<String> saveOrderItems(@RequestBody List<OrderItem> orderItemList) {
        ResultUtil<String> saveResult = orderItemService.saveByList(orderItemList);
        return saveResult;
    }


    /**
     * 获取订单详情
     * @param orderNo
     * @return
     */
    @GetMapping("/{orderNo}")
    @ResponseBody
    public ResultUtil<Order> detail(@PathVariable("orderNo") String orderNo) {
        Order order = orderService.getByOrderNo(orderNo);
        if (Objects.nonNull(order)) {
            return new ResultUtil<>(200, "查询成功", order);
        } else {
            return new ResultUtil<>(201, "查询失败，请联系管理员", null);
        }
    }


    /**
     * 前台用户订单列表页，支持翻页
     * @param requestParam
     * @return
     */
    @GetMapping("/orders")
    @ResponseBody
    public ResultUtil<PageResultUtil<Order>> orders(@RequestBody OrdersRequestParam requestParam) {
        Integer userId = requestParam.getUserId();
        Integer page = requestParam.getPage();
        Integer limit = requestParam.getLimit();
        Integer isDeleted = requestParam.getIsDeleted();

        PageResultUtil<Order> userOrderItemList = orderService.findUserOrderItemList(userId, page, limit, isDeleted);
        if (Objects.nonNull(userOrderItemList)) {
            return new ResultUtil<>(200, "查询成功", userOrderItemList);
        } else {
            return new ResultUtil<>(201, "查询失败，请稍后再试");
        }
    }
}
